package com.jerei.web.utils.ui;

import com.alibaba.fastjson.JSONObject;
import com.jerei.utils.UtilsCommon;
import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.ext.beans.SimpleMapModel;
import freemarker.template.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by sunfeihu on 2016/10/26.
 */
@Component
public class ButtonForGridTag implements TemplateDirectiveModel {

    @Autowired
    private HttpServletRequest request;

    private LinkedHashMap<String, JSONObject> getBtnMap() {
        LinkedHashMap<String, JSONObject> btnMap = new LinkedHashMap<String, JSONObject>();
        JSONObject obj = new JSONObject();
        obj.put("name", "增加");
        obj.put("url", "edit");
        obj.put("icon", "add");
        obj.put("options", new JSONObject());
        obj.put("inAll", true);
        btnMap.put("add", obj);

        obj = new JSONObject();
        obj.put("name", "删除");
        obj.put("url", "delete");
        obj.put("icon", "remove");
        obj.put("inAll", true);
        obj.put("options", new JSONObject());
        btnMap.put("remove", obj);

        obj = new JSONObject();
        obj.put("name", "查询");
        obj.put("icon", "search");
        obj.put("inAll", true);
        obj.put("options", new JSONObject());
        btnMap.put("search", obj);

        obj = new JSONObject();
        obj.put("name", "打印");
        obj.put("url", "print");
        obj.put("icon", "print");
        obj.put("inAll", true);
        obj.put("options", new JSONObject());
        btnMap.put("print", obj);

        obj = new JSONObject();
        obj.put("name", "导出");
        obj.put("url", request.getContextPath() + "/admin/export");
        obj.put("icon", "export");
        obj.put("inAll", true);
        obj.put("options", new JSONObject());
        btnMap.put("export", obj);

        obj = new JSONObject();
        obj.put("name", "排序");
        obj.put("url", request.getContextPath() + "/admin/sort");
        obj.put("icon", "sort");
        obj.put("inAll", true);
        obj.put("options", new JSONObject());
        btnMap.put("sort", obj);

        obj = new JSONObject();
        obj.put("name", "上传");
        obj.put("icon", "upload");
        obj.put("url", request.getContextPath() + "/admin/wcm/common/resource/upload");
        obj.put("inAll", false);
        obj.put("options", new JSONObject());
        btnMap.put("upload", obj);

        return btnMap;
    }

    @Override
    public void execute(Environment env, Map params, TemplateModel[] models, TemplateDirectiveBody body) throws TemplateException, IOException {
        String btns = UtilsCommon.getString(params.get("btns"));
        String urls = UtilsCommon.getString(params.get("urls"));
        String options = UtilsCommon.getString(params.get("options"));
        JSONObject optionsObj = null;
        if (null != options && !"".equals(options)) {
            optionsObj = JSONObject.parseObject(options);
        }
        LinkedHashMap<String, JSONObject> btnMap = this.getBtnMap();
        if (null != urls && !"".equals(urls)) {
            String[] arr = urls.split(",");
            String[] _kv = null;
            for (int i = 0; i < arr.length; i++) {
                _kv = arr[i].split(":");
                if (_kv[1].startsWith("'") && _kv[1].endsWith("'")) {
                    _kv[1] = _kv[1].substring(1, _kv[1].length() - 1);
                }
                if (_kv[1].startsWith("/")) {
                    btnMap.get(_kv[0]).put("url", request.getContextPath() + _kv[1]);
                } else {
                    btnMap.get(_kv[0]).put("url", _kv[1]);
                }
            }
        }
        BeansWrapper beansWrapper = new BeansWrapperBuilder(Configuration.getVersion()).build();
        LinkedHashMap<String, JSONObject> canUseBtnMap = new LinkedHashMap<String, JSONObject>();
        //只要不是全部，进行按钮筛选
        if (!"all".equals(btns)) {
            String[] btnArr = btns.split(",");
            JSONObject btn = null;
            for (int i = 0; i < btnArr.length; i++) {
                btn = btnMap.get(btnArr[i]);
                if (null != btn) {
                    if (null != optionsObj && null != optionsObj.get(btnArr[i])) {
                        btn.put("options", optionsObj.get(btnArr[i]));
                    }
                    canUseBtnMap.put(btnArr[i], btn);
                }
            }
        } else {//如果是全部按钮
            JSONObject btn = null;
            for (String btnKey : btnMap.keySet()) {
                btn = btnMap.get(btnKey);
                if (null != btn && btn.getBoolean("inAll")) {
                    canUseBtnMap.put(btnKey, btn);
                }
            }
        }
        env.setLocalVariable("btnMap", new SimpleMapModel(canUseBtnMap, beansWrapper));
        Writer out = env.getOut();
        if (body != null) {
            body.render(env.getOut());
        } else {
            throw new RuntimeException("标签内部至少要加一个空格");
        }
    }
}